package in.ac.dei.edrp.cms.controller.prestaging;

import in.ac.dei.edrp.cms.dao.prestaging.PrestagingService;
import in.ac.dei.edrp.cms.domain.coursegroup.CourseGroupBean;
import in.ac.dei.edrp.cms.domain.coursemaster.CourseMasterBean;
import in.ac.dei.edrp.cms.domain.registration.prestaging.TransferPSTRDYInSTBean;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import jxl.Sheet;
import jxl.Workbook;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class PrestagingController extends MultiActionController {
	private PrestagingService prestagingService;

	public void setPrestagingService(PrestagingService prestagingService) {
		this.prestagingService = prestagingService;
	}

	public ModelAndView getEntityList(HttpServletRequest request,
			HttpServletResponse response) {
		HttpSession session = request.getSession(true);
		if (session.getAttribute("universityId") == null) {
			return new ModelAndView("general/SessionInactive",
					"sessionInactive", true);
		}

		CourseGroupBean prestagingInfoGetter = new CourseGroupBean();
		prestagingInfoGetter.setParentEntity(session.getAttribute("userId")
				.toString().substring(1, 9));
		prestagingInfoGetter.setUniversityId(session.getAttribute(
				"universityId").toString());
		String mode = request.getParameter("mode");

		if ((mode != null) && (mode != "") && mode.equalsIgnoreCase("all")) {
			prestagingInfoGetter.setParentEntity("%");
		}

		List<CourseMasterBean> entityList = prestagingService
				.getChildEntityList(prestagingInfoGetter);

		return new ModelAndView("coursemaster/componentInfo", "detailsList",
				entityList);
	}

	public ModelAndView getProgramList(HttpServletRequest request,
			HttpServletResponse response) {
		HttpSession session = request.getSession(true);
		if (session.getAttribute("universityId") == null) {
			return new ModelAndView("general/SessionInactive",
					"sessionInactive", true);
		}
		List<CourseMasterBean> programList = prestagingService
				.getProgramList(request.getParameter("entityId"));
		return new ModelAndView("coursemaster/componentInfo", "detailsList",
				programList);
	}

	public ModelAndView getBranchList(HttpServletRequest request,
			HttpServletResponse response) {
		HttpSession session = request.getSession(true);
		if (session.getAttribute("universityId") == null) {
			return new ModelAndView("general/SessionInactive",
					"sessionInactive", true);
		}

		CourseGroupBean prestagingInfoGetter = new CourseGroupBean();
		prestagingInfoGetter.setEntityId(request.getParameter("entityId"));
		prestagingInfoGetter.setProgramId(request.getParameter("programId"));

		List<CourseMasterBean> branchList = prestagingService
				.getBranchList(prestagingInfoGetter);

		return new ModelAndView("coursemaster/componentInfo", "detailsList",
				branchList);
	}

	public ModelAndView getSpecializationList(HttpServletRequest request,
			HttpServletResponse response) {
		HttpSession session = request.getSession(true);
		if (session.getAttribute("universityId") == null) {
			return new ModelAndView("general/SessionInactive",
					"sessionInactive", true);
		}
		CourseGroupBean prestagingInfoGetter = new CourseGroupBean();
		prestagingInfoGetter.setEntityId(request.getParameter("entityId"));
		prestagingInfoGetter.setProgramId(request.getParameter("programId"));
		prestagingInfoGetter.setBranchId(request.getParameter("branchId"));

		List<CourseMasterBean> specializationList = prestagingService
				.getSpecializationList(prestagingInfoGetter);

		return new ModelAndView("coursemaster/componentInfo", "detailsList",
				specializationList);
	}

	public ModelAndView setPrestagingData(HttpServletRequest request,
			HttpServletResponse response) {
		HttpSession session = request.getSession(true);
		if (session.getAttribute("universityId") == null) {
			return new ModelAndView("general/SessionInactive",
					"sessionInactive", true);
		}
		String fileName = this.getServletContext().getRealPath("/")
				+ "PrestagingDataDocuments/" + request.getParameter("fileName");

		List<TransferPSTRDYInSTBean> inputPrestagingData = new ArrayList<TransferPSTRDYInSTBean>();
		File inputWorkbook = new File(fileName);
		Workbook w;
		try {
			w = Workbook.getWorkbook(inputWorkbook);

			for (int s = 0; s < w.getNumberOfSheets(); s++) {
				Sheet sheet = w.getSheet(s);

				for (int i = 2; i < sheet.getRows(); i++) {
					if ((sheet.getCell(1, i).getContents() == "")
							|| (sheet.getCell(1, i).getContents() == null)) {
						continue;
					}

					TransferPSTRDYInSTBean prestagingData = new TransferPSTRDYInSTBean();

					prestagingData.setStudentId(sheet.getCell(0, i)
							.getContents());
					prestagingData.setRollNumber(sheet.getCell(1, i)
							.getContents());
					prestagingData.setStudentFirstName(sheet.getCell(2, i)
							.getContents());
					prestagingData.setStudentMiddleName(sheet.getCell(3, i)
							.getContents());
					prestagingData.setStudentLastName(sheet.getCell(4, i)
							.getContents());
					prestagingData.setFatherFirstName(sheet.getCell(5, i)
							.getContents());
					prestagingData.setFatherMiddleName(sheet.getCell(6, i)
							.getContents());
					prestagingData.setFatherLastName(sheet.getCell(7, i)
							.getContents());
					prestagingData.setMotherFirstName(sheet.getCell(8, i)
							.getContents());
					prestagingData.setMotherMiddleName(sheet.getCell(9, i)
							.getContents());
					prestagingData.setMotherLastName(sheet.getCell(10, i)
							.getContents());
					prestagingData.setDateOfBirth(sheet.getCell(11, i)
							.getContents());
					prestagingData.setCategoryCode(sheet.getCell(12, i)
							.getContents());
					prestagingData
							.setGender(sheet.getCell(13, i).getContents());
					prestagingData
							.setEntityId(request.getParameter("entityId"));
					prestagingData.setProgramId(request
							.getParameter("programId"));
					prestagingData.setNewBranch(request
							.getParameter("branchId"));
					prestagingData.setNewSpecialization(request
							.getParameter("specializationId"));
					prestagingData.setSemesterCode(request
							.getParameter("semesterId"));
					prestagingData.setSemesterStartDate(request
							.getParameter("semesterStartDate"));
					prestagingData.setSemesterEndDate(request
							.getParameter("semesterEndDate"));
					prestagingData.setPrimaryEmailId(sheet.getCell(14, i)
							.getContents());
					prestagingData.setPerAddress(sheet.getCell(15, i)
							.getContents());
					prestagingData.setPerCity(sheet.getCell(16, i)
							.getContents());
					prestagingData.setPerState(sheet.getCell(17, i)
							.getContents());
					prestagingData.setPerPincode(sheet.getCell(18, i)
							.getContents());
					prestagingData.setCorAddress(sheet.getCell(19, i)
							.getContents());
					prestagingData.setCorCity(sheet.getCell(20, i)
							.getContents());
					prestagingData.setCorState(sheet.getCell(21, i)
							.getContents());
					prestagingData.setCorPincode(sheet.getCell(22, i)
							.getContents());
					prestagingData.setOfficePhone(sheet.getCell(23, i)
							.getContents());
					prestagingData.setExtraPhone(sheet.getCell(24, i)
							.getContents());
					prestagingData.setOtherPhone(sheet.getCell(25, i)
							.getContents());
					prestagingData.setFax(sheet.getCell(26, i).getContents());
					prestagingData.setAdmissionMode("New");
					prestagingData.setAttemptNumber(1);
					prestagingData.setCreatorId(session
							.getAttribute("userName").toString());
					String registrationDueDate = prestagingService
							.getRegistrationDueDate(prestagingData);

					prestagingData.setRegisterDueDate(registrationDueDate);

					inputPrestagingData.add(prestagingData);
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}

		List<TransferPSTRDYInSTBean> insertedPrestagingData = prestagingService
				.setPrestagingData(inputPrestagingData);
		if (insertedPrestagingData.size() > 0) {
			String s = this.getServletContext().getRealPath("/")
					+ "PrestagingDataDocuments/"
					+ request.getParameter("fileName");
			File source = new File(s);
			s = this.getServletContext().getRealPath("/")
					+ "UploadedPrestagingDataDocuments/";
			File destination = new File(s);
			destination.mkdir();
			s = this.getServletContext().getRealPath("/")
					+ "UploadedPrestagingDataDocuments/"
					+ request.getParameter("fileName");
			destination = new File(s);
			source.renameTo(destination);
		}

		return new ModelAndView("prestaging/insertInfo", "dataList",
				insertedPrestagingData);
	}
}